/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import React, { useRef } from 'react';
import type { ActionType, ProColumns } from '@ant-design/pro-table';
import ProTable from '@ant-design/pro-table';
import DragTable from '@/components/DragTable';
import { getExcelList } from '@/services/Finance/jdkDeliveryTransfer';
import { usePageTabsChange } from '@/utils/hooks/pageTabs';
import { useUpdateEffect } from 'ahooks';
import { Space } from 'antd';

interface JdkVoiceIndexProps {
  activeKey: boolean;
}
interface VoiceType {
  id: string;
  created_at: string;
  file_name: string;
  file_path: string;
  status: string;
  user: string;
}

const JdkVoiceIndex: React.FC<JdkVoiceIndexProps> = (props) => {
  const [show] = usePageTabsChange();
  const { activeKey } = props;
  const actionRef = useRef<ActionType>();

  const columns: ProColumns<VoiceType>[] = [
    {
      title: '对账单名称',
      dataIndex: 'file_name',
      align: 'center',
      width: '20%',
    },
    {
      title: '操作人',
      dataIndex: 'user',
      align: 'center',
      width: '20%',
    },
    {
      title: '操作时间',
      dataIndex: 'created_at',
      align: 'center',
      width: '30%',
    },
    {
      title: '文件状态',
      dataIndex: 'status',
      align: 'center',
      width: '20%',
      valueEnum: {
        '0': '生成中',
        '1': '生成中',
        '2': '已生成',
        '3': '生成失败',
      },
    },
    {
      title: '操作',
      dataIndex: 'file_path',
      align: 'center',
      width: '10%',
      render: (_, record) => {
        const { status, file_path } = record;
        return status == '2' ? file_path ? <a href={file_path}>下载</a> : '暂无数据' : '-';
      },
    },
  ];

  useUpdateEffect(() => {
    if (show && activeKey) {
      actionRef.current?.reload();
    }
  }, [show, activeKey]);

  return (
    <DragTable>
      <ProTable
        cardProps={{
          title: <Space>下载的文件最多保存15天，请尽快下载</Space>,
        }}
        rowKey='id'
        actionRef={actionRef}
        request={getExcelList}
        columns={columns}
        pagination={false}
        search={false}
        scroll={{
          x: 900,
        }}
        options={false}
      />
    </DragTable>
  );
};

export default JdkVoiceIndex;
